﻿namespace Future.Web.Controllers
{
    /// <summary>
    /// 用户业务控制器
    /// </summary>
    public class UserController : AuthorizeController
    {
        private readonly IUserBLL _userBLL;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="userBLL"></param>
        public UserController(IUserBLL userBLL)
        {
            _userBLL = userBLL;
        }

        /// <summary>
        /// 获取当前登录用户的信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public UserInfoVo GetUserAllInfo()
        {
            return _userBLL.GetUserAllInfo(CurrentUser.User.Id);
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="userDto"></param>
        /// <returns></returns>
        [HttpPost]
        [Permission("system:user:add")]
        public bool Add(UserInfoDto userDto)
        {
            if (string.IsNullOrEmpty(userDto.User.Password))
            {
                throw new Exception("密码为空，添加失败！");
            }

            return _userBLL.AddInfo(userDto);
        }

        /// <summary>
        /// 通过id获取用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("{id}")]
        public UserInfoDto GetById(Guid id)
        {
            return _userBLL.GetUserById(id);
        }

        /// <summary>
        /// 获取用户已授权的角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("{id}")]
        public UserInfoDto AuthRole(Guid id)
        {
            return _userBLL.AuthRole(id);
        }

        /// <summary>
        /// 用户授权角色
        /// </summary>
        /// <param name="userDto"></param>
        /// <returns></returns>
        [HttpPut]
        [Permission("system:user:auth")]
        public bool UpdateAuthRole(UserInfoDto userDto)
        {
            return _userBLL.UpdateAuthRole(userDto.UserId, userDto.RoleIds);
        }

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="userDto"></param>
        /// <returns></returns>
        [HttpPut]
        [Permission("system:user:edit")]
        public bool Update(UserInfoDto userDto)
        {
            return _userBLL.UpdateInfo(userDto);
        }

        /// <summary>
        /// 更改用户状态
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        [HttpPut]
        public bool UpdateStatus(Guid userId, bool status)
        {
            return _userBLL.UpdateStatus(userId, status);
        }

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="userModel"></param>
        /// <returns></returns>
        [HttpPut]
        public bool UpdateUserProfile(UserModel userModel)
        {
            return _userBLL.UpdateUserProfile(userModel);
        }

        /// <summary>
        /// 更新用户头像
        /// </summary>
        /// <param name="userModel"></param>
        /// <returns></returns>
        [HttpPut]
        public bool UpdateUserAvatar(UserModel userModel)
        {
            return _userBLL.UpdateUserAvatar(userModel);
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPut]
        public bool RestPassword(UserModel user)
        {
            return _userBLL.RestPassword(user.Id, user.Password);
        }

        /// <summary>
        /// 用户更新密码
        /// </summary>
        /// <param name="userResetPwdDto"></param>
        /// <returns></returns>
        [HttpPut]
        public bool UpdatePassword(UserResetPwdDto userResetPwdDto)
        {
            return _userBLL.UpdatePassword(CurrentUser.User.Id, userResetPwdDto);
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        [Permission("system:user:remove")]
        public bool DeleteUserList(List<Guid> ids)
        {
            return _userBLL.DeleteUserList(ids);
        }

        /// <summary>
        /// 分页获取用户信息列表
        /// </summary>
        /// <param name="queryDto"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        [HttpGet]
        [Permission("system:user:query")]
        public PageModel<UserModel> PageList([FromQuery] UserQueryDto queryDto, [FromQuery] PageParModel page)
        {
            PageModel<UserModel> result = _userBLL.SelctPageList(queryDto, page);
            return result;
        }

        /// <summary>
        /// 查询角色已授权用户分页列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public PageModel<UserModel> AllocatedUserPageList([FromQuery] UserQueryDto userModel, [FromQuery] PageParModel page)
        {
            PageModel<UserModel> result = _userBLL.AllocatedUserPageList(userModel, page);
            return result;
        }

        /// <summary>
        /// 查询角色未授权用户列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public PageModel<UserModel> UnallocatedPageList([FromQuery] UserQueryDto userQueryDto, [FromQuery] PageParModel page)
        {
            PageModel<UserModel> result = _userBLL.UnallocatedPageList(userQueryDto, page);
            return result;
        }
    }
}
